package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.net.http.HttpRequestHeader;
import com.ts.api.http.org.entity.apply.OrgApplyHuaQiData;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  安心分期 花旗北京
 *
 *  @author WJB
 */
@ApiPlan(productId = "15", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HuaQi implements ApiOrgPhoneMd5ConvertHandler {
    private static final String TO_KEN = "35e54adad9858e73a30417b946938a53";
    private static final String ASE_KEY = "581592408ae8a82c";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        HttpRequestHeader httpRequestHeader = HttpRequestHeader.create("token", TO_KEN);
        apiDataExchangeParameter.setHttpRequestHeader(httpRequestHeader);

        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile", apiDataExchangeParameter.getPhoneMd5());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        JSONObject data = jsonObject.getJSONObject("data");
        boolean code = Eq.object(data.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        HttpRequestHeader httpRequestHeader = HttpRequestHeader.create("token", TO_KEN);
        apiApplyParameter.setHttpRequestHeader(httpRequestHeader);

        OrgApplyHuaQiData data = new OrgApplyHuaQiData();
        data.setName(apiApplyParameter.getUserName());
        data.setMobile(apiApplyParameter.getPhone());
        //  城市编码
        data.setCity(apiApplyParameter.getCity());
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  贷款金额
        data.setAmount(apiFieldConverter.getLoanAmountUnit());
        //  扩展信息
        data.setExtend(apiFieldConverter.getExtendInfo());
        AES ase = AES.createEcb(ASE_KEY.getBytes());
        String dataStr = ase.encryptToBase64Str(JsonUtils.toJson(data));
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("requestNo", apiApplyParameter.getReqId());
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        JSONObject data = jsonObject.getJSONObject("data");
        if (data == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }

        boolean code = Eq.object(data.getInteger("code"), 1);
        if (!code) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
